package com.hcss.model.service.impl;

import java.util.Date;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hcss.common.base.BaseClass;
import org.apache.commons.collections.CollectionUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hcss.paging.service.SchemaPagingByExample;
import com.hcss.util.EmptyUtil;
import com.hcss.model.dao.ModelMapper;
import com.hcss.model.model.Model;
import com.hcss.model.model.ModelExample;
import com.hcss.model.model.par.ModelPar;
import com.hcss.model.service.ModelService;

/**
 * @描述: 模型的model层-服务的实现类
 * @作者: guoxia
 * @创建日期: 2023-05-16 15:14:21
 */
@Service("modelService")
public class ModelServiceImpl implements ModelService {
	
	@Autowired
	private ModelMapper modelMapper;

	@Override
	public PageInfo<Model> queryPagingData(ModelPar par) throws Exception{
        ModelExample example = (ModelExample)SchemaPagingByExample.getBaseExample(ModelExample.class, par);
        PageHelper.startPage(par.getPage(), par.getPerpage());
        List<Model> list =  modelMapper.selectByExample(example);
        return new PageInfo<>(list);
	}
	
	@Override
	public List<Model> queryAllData(ModelPar par) throws Exception {
		ModelExample example = (ModelExample)SchemaPagingByExample.getBaseExample(ModelExample.class, par);
		return modelMapper.selectByExample(example);
	}

	@Override
	public void insertModel(Model model) {
		model.setId( BaseClass.getUUIDWithoutConnector() );
		model.setCreatetime( new Date(  ) );
		model.setCreateuser( BaseClass.getSessionUserid() );
		model.setIfactive( "0" );
		modelMapper.insertSelective(model);
	}

	@Override
	public void updateModel(Model model) {
        ModelExample example = new ModelExample();
        example.createCriteria().andIdEqualTo(model.getId());
        // TODO 补充更新条件
        modelMapper.updateByExampleSelective(model, example);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		ModelExample example = new ModelExample();
		example.createCriteria().andIdIn(ids);
		modelMapper.deleteByExample(example);
	}
	
	@Override
	public Model selectByPK(String id) {
		if (EmptyUtil.isHaveEmpty(id)) return null;

        ModelExample example = new ModelExample();
//        example.createCriteria().andIdEqualTo(id);
		List<Model> list = modelMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}
}
